<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="家政类型" prop="homeId">
        <el-select
          v-model="queryParams.homeId"
          placeholder="请选择家政类型"
          style="width: 100%"
        >
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="展示图标" prop="icon">
				<el-input v-model="queryParams.icon" placeholder="请输入展示图标" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item> -->
      <!-- <el-form-item label="商品标签" prop="shoppingDicts">
				<el-input v-model="queryParams.shoppingDicts" placeholder="请输入商品标签" clearable
					@keyup.enter.native="handleQuery" />
				<el-select v-model="queryParams.shoppingDicts" placeholder="请选择">
					<el-option v-for="item in tagList" :key="item.value" :label="item.dictLabel" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item> -->
      <el-form-item label="商品打分" prop="mark">
        <el-input
          v-model="queryParams.mark"
          placeholder="请输入商品打分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论数量" prop="observeNumber">
        <el-input
          v-model="queryParams.observeNumber"
          placeholder="请输入评论数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="评论标签" prop="evaluateDicts">
				<el-input v-model="queryParams.evaluateDicts" placeholder="请输入评论标签" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:product:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:product:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:product:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:product:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="productList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="类型" align="center" prop="typeName" />
      <el-table-column label="展示图标" align="center">
        <template slot-scope="scope">
          <image-preview
            :src="api_host + scope.row.icon"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="明细展示图" align="center">
        <template slot-scope="scope">
          <image-preview
            :src="api_host + scope.row.images"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="订单量" align="center" prop="orderVolume" />
      <el-table-column label="支付量" align="center" prop="payVolume" />
      <el-table-column label="浏览量" align="center" prop="viewVolume" />
      <!-- <el-table-column label="商品标签" align="center" prop="shoppingDicts" /> -->
      <el-table-column label="商品打分" align="center" prop="mark" />
      <el-table-column label="评论数量" align="center" prop="observeNumber" />
      <el-table-column label="虚拟销售量" align="center" prop="salesVolume" />
      <el-table-column label="实际销售量" align="center" prop="salesCount" />
      <!-- <el-table-column label="评论标签" align="center" prop="evaluateDicts" /> -->
      <el-table-column label="是否热门" align="center" prop="isHot" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isHot" active-color="#13ce66" :active-value="1" :inactive-value="0"
						inactive-color="#999" @change="changeisDis(scope.row)">
					</el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否上架" align="center" prop="isHot" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" :active-value="0" :inactive-value="1"
						inactive-color="#999" @change="changeStatus(scope.row)">
					</el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:product:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:product:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家政产品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="title" required>
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="所属类目" prop="homeId" required>
          <el-select
            v-model="form.homeId"
            placeholder="请选择类目"
            style="width: 100%"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="展示图标" prop="icon" required>
          <image-upload v-model="form.icon" :limit="1" />
        </el-form-item>
        <el-form-item label="明细展示图" prop="images" required>
          <image-upload v-model="form.images" />
        </el-form-item>
        <el-form-item label="展示视频" prop="video">
          <file-upload
            v-model="form.video"
            :limit="1"
            :fileType="['mp4']"
          ></file-upload>
          <!-- <image-upload v-model="form.icon" :limit="1" /> -->
        </el-form-item>
        <el-form-item label="商品标签" prop="shoppingDicts" required>
          <el-select
            v-model="form.shoppingDicts"
            multiple
            placeholder="请选择商品标签"
            style="width: 100%"
          >
            <el-option
              v-for="item in tagList"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="String(item.dictCode)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品打分" prop="mark">
          <el-input v-model="form.mark" placeholder="请输入商品打分" />
        </el-form-item>
        <el-form-item label="评论数量" prop="observeNumber">
          <el-input v-model="form.observeNumber" placeholder="请输入评论数量" />
        </el-form-item>
        <el-form-item label="虚拟销售量" prop="salesVolume">
          <el-input v-model="form.salesVolume" placeholder="请输入虚拟销售量" />
        </el-form-item>
        <el-form-item label="评论标签" prop="evaluateDicts" required>
          <el-select
            v-model="form.evaluateDicts"
            multiple
            placeholder="请选择商品标签"
            style="width: 100%"
          >
            <el-option
              v-for="item in pltagList"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="String(item.dictCode)"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否上架" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">是</el-radio>
            <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="规则选择形式" prop="isMulti">
          <el-radio-group v-model="form.isMulti">
            <el-radio :label="1">单选</el-radio>
            <el-radio :label="2">多选且可选择数量</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="规格" required>
          <el-table :data="form.detailList" border style="width: 100%">
            <el-table-column label="名称">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.proName"
                  placeholder="请输入规格名称"
                />
              </template>
            </el-table-column>
            <el-table-column label="价格">
              <template slot-scope="scope">
                <el-input
                  type="number"
                  v-model="scope.row.proPrice"
                  placeholder="请输入价格"
                />
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  v-if="form.detailList.length > 1"
                  @click="skuAction(1, scope.$index)"
                  >删除</el-button
                >
                <el-button
                  size="mini"
                  type="success"
                  v-if="form.detailList.length - 1 == scope.$index"
                  @click="skuAction(2, scope.$index)"
                  >添加</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="商品详情" prop="proDesc" required>
          <editor v-model="form.proDesc" :min-height="192" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
} from "@/api/system/product";
import { listType } from "@/api/system/type";
import { listData } from "@/api/system/dict/data";
export default {
  name: "Product",
  data() {
    return {
      typeList: [],
      tagList: [],
      pltagList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家政产品信息表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        homeId: null,
        icon: null,
        images: null,
        shoppingDicts: null,
        mark: null,
        observeNumber: null,
        evaluateDicts: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getTypes();
    this.getTags();
    this.getplTags();
    this.getList();
  },
  methods: {
    /** 查询家政类型列列表 */
    getTypes() {
      this.loading = true;
      listType({
        pageSize: 9999,
      }).then((response) => {
        this.typeList = response.rows;
      });
    },
    /** 查询字典数据列表 */
    getTags() {
      this.loading = true;
      listData({
        dictType: "hkeep_shopping_label",
        pageSize: 9999,
      }).then((response) => {
        this.tagList = response.rows;
      });
    },
    /** 查询评论标签列表 */
    getplTags() {
      this.loading = true;
      listData({
        dictType: "hkeep_evaluate_label",
        pageSize: 9999,
      }).then((response) => {
        this.pltagList = response.rows;
      });
    },
    /** 查询家政产品信息列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        homeId: null,
        icon: null,
        images: null,
        shoppingDicts: null,
        mark: null,
        observeNumber: null,
        evaluateDicts: null,
        isMulti: 1,
        detailList: [
          {
            proName: "",
            proPrice: "",
          },
        ],
      };
      this.resetForm("form");
    },
    /** 规格数据操作 */
    skuAction(type, index) {
      if (type == 1) {
        this.form.detailList.splice(index, 1);
      } else {
        this.form.detailList.push({
          proName: "",
          proPrice: "",
        });
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家政产品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProduct(id).then((response) => {
        this.form = response.data;
        this.form.shoppingDicts = this.form.shoppingDicts
          ? this.form.shoppingDicts.split(",")
          : "";
        this.form.evaluateDicts = this.form.evaluateDicts
          ? this.form.evaluateDicts.split(",")
          : "";
        console.log(this.form);
        this.open = true;
        this.title = "修改家政产品信息";
      });
    },
    //修改是否热门状态
    changeisDis(row){
      updateProduct(row).then(res=>{
        this.$modal.msgSuccess("修改成功");
        this.getList();
      })
    },
    //修改是否上架状态
    changeStatus(row){
      updateProduct(row).then(res=>{
        this.$modal.msgSuccess("修改成功");
        this.getList();
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const shoppingDicts = this.form.shoppingDicts.join(",");
          const evaluateDicts = this.form.evaluateDicts.join(",");
          this.form.shoppingDicts = shoppingDicts;
          this.form.evaluateDicts = evaluateDicts;
          if (this.form.id != null) {
            updateProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除家政产品信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProduct(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/product/export",
        {
          ...this.queryParams,
        },
        `product_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
